// pages/mall/mall.js
var app = getApp();
var util = require('../../utils/util.js')
const key = 'KTPBZ-XKYCK-ZNAJX-AZCIA-WNXQK-5DFEX' //地理位置的key值
Component({
  /**
   * 组件的初始数据
   */
  data: {
    imgUrls: [
    ],
    params : {
      pageNum:1,
      pageSize:1000,
      model:'φ18-22 定尺'
    },
    circular: true,
    //是否显示画板指示点 
    indicatorDots: true,
    //选中点的颜色 
    //是否竖直 
    vertical: false,
    //是否自动切换 
    autoplay: true,
    //自动切换的间隔
    interval: 3000,
    //滑动动画时长毫秒 
    duration: 1000,
    //所有图片的高度 
    imgheights: 280,
    scrollHeight:700,
    //图片宽度 
    imgwidth: 750,
    //默认 
    current: 0,
    date:'2020-08-14',
    today:'',
    areaList:[],
    areaName:'湖北省',
    cityAreaArray:[],
    brandindex:-1,
    materialindex:-1,
    citysIndex: [0, 0],
    noticeList:[],
    list: [],
    brandList: [],
    brandStrList:[],
    caiziList: [],
    normList: [],
    totalYunFeeList:[],
    timer: null, // 保存定时器
    scrollTop: 5 // 设定触发条件的距离
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      //获取今天日期
      const dateUtils = require('../../utils/util.js');
      var today = dateUtils.formatDate(new Date());
      var windowHeight = app.globalData.windowHeight;
      var tabHeight = ((44 + app.globalData.systemInfo.statusBarHeight) * (750 / app.globalData.systemInfo.windowWidth)); //将高度乘以换算后的该设备的rpx与px的比例
      var height = windowHeight - this.data.imgheights - 120 - tabHeight - 164;

      this.setData({
        date: today,
        today: today,
        scrollHeight: height
      })
      var that = this;
      var token =  wx.getStorageSync('token');
      if(token == null || token == ''){
        wx.reLaunch({
          url: '/pages/login/login',
        })
        return;
      }
      that.showAreaData();
      that.getNotice();
      that.getBanner();
      that.getTree();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    setLoc(area){
      this.setData({
        areaName:area
      })
      this.getAddressPriceList();
    },
    showPicker(id,name){
      this.setData({
        areaName: name
      })
      this.getAddressPriceList();
    },
    bindPickerChange1(val){
      var index = Number(val.detail.value);
      var materialId = "";
      if(index>0){
        materialId = this.data.caiziList[index-1].dictName;
      }
      let material = `params.material`;
      this.setData({
        [material]:materialId,
        materialindex:index
      })
      this.getPrice();
    },
    bindPickerChange2(val){
      var index = Number(val.detail.value);
      var brandId = "";
      if(index>0){
        brandId = this.data.brandList[index-1].dictName;
      }
      let brand = `params.brand`;
      this.setData({
        [brand]:brandId,
        brandindex:index
      })
      this.getPrice();
    },
    showDetail(e){
      console.log(e);
      var name = e.currentTarget.dataset.name;
      var areaId = this.data.params.areaId;
      wx.navigateTo({
        url: '/pages/malldetail/malldetail?name=' + name +"&areaId="+areaId,
      })

    },
    //获取字典
    getTree(){
      var that = this;
      app.request.postHeadForm('hgz/dict/queryDictAndSubById/4').then(res =>{
        that.setData({
            totalYunFeeList:res.data.children
        })
      })
      app.request.postHeadForm('hgz/dict/getTree').then(res => {
        var brandList = [];
        var normList = [];
        var caiziList = [];
        var weightList = [];
        var brandStrList = [];
        var materialStrList = [];
        brandStrList.push('全部');
        materialStrList.push('全部');
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].dictName == '品牌') {
            brandList = res.data[i].children;
            for(var j=0;j<brandList.length;j++){
              brandStrList.push(brandList[j].dictName);
            }
          } else if (res.data[i].dictName == '材质') {
            caiziList = res.data[i].children;
            for(var j=0;j<caiziList.length;j++){
              materialStrList.push(caiziList[j].dictName);
            }
          }
        }
        that.setData({
          brandList: brandList,
          caiziList: caiziList,
          brandStrList:brandStrList,
          materialStrList:materialStrList
        })
      });
    },
    //获取价格
    getPrice(){
      console.log(this.data.params);
      app.request.postHeadForm('hgz/product/queryListWithPage', this.data.params)
      .then(res =>{
        if(res.data.list == null || res.data.list.length == 0){
          wx.showToast({
            title: '暂无价格记录',
            icon: 'none',
            duration: 1000
          })
          this.setData({
            list: []
          })
          return;
        }
        var totalYunFeeList = this.data.totalYunFeeList;
        var dataList = res.data.list;
        for (var i = 0; i < dataList.length;i++){
          for(var j=0;j<totalYunFeeList.length;j++){
            if(dataList[i].brand == totalYunFeeList[j].dictValue){
              for(var k=0;k<totalYunFeeList[j].children.length;k++){
                if(dataList[i].areaName == totalYunFeeList[j].children[k].dictName){
                  dataList[i].price = dataList[i].price + Number(totalYunFeeList[j].children[k].dictValue);
                  break;
                }
              }
              break;
            }
          }
          dataList[i].updateTm = dataList[i].updateTime.substring(5,16);
          dataList[i].brandName = dataList[i].brand;
          dataList[i].materialName = dataList[i].material;
          dataList[i].modelName = dataList[i].model;
        }
        var newList = [];
        for (var k = 0; k < dataList.length;k++){
          if (dataList[k].brandName != null && 
            dataList[k].modelName != null && 
            dataList[k].materialName != null ){
            newList.push(dataList[k])
          }
        }
        if(newList.length == 0){
          wx.showToast({
            title: '暂无价格记录',
            icon: 'none',
            duration: 1000
          })
        }
        this.setData({
          list: newList
        })
      })
    },
    getPriceList(areaId){
      let areas = `params.areaId`;
      this.setData({
        [areas]:areaId
      })
      this.getPrice();
    },


    getNotice(){
      var that = this;
      var params = {
        pageNum:1,
        pageSize:10
      }
      app.request.postHeadForm('hgz/notice/queryListWithPage',params).then(res=>{
        if (res.status == true && res.data.list!=null){
          var notice =  res.data.list;
          that.setData({
            noticeList: notice
          })
         }
      })
    },

    clickBanner(e){
      // var url = e.currentTarget.dataset.url;
      // wx.navigateTo({
      //   url: '/pages/notice/notice?noticeId=' + res.currentTarget.id,
      // })
    },
    // 跳转至公告页面
    tapNotice(res) {
      wx.navigateTo({
        url: '/pages/notice/notice?noticeId=' + res.currentTarget.id,
      })
    },
    getBanner(){
      var that = this;
      app.request.postHeadForm('hgz/rotation/queryListWithPage').then(res => {
        that.setData({
          imgUrls: res.data.list
        })
      }).catch(err=>{
        if(err.statusCode == 401){
          wx.reLaunch({
            url: '/pages/login/login',
          })
        }
      })
    },
    getAddressPriceList(){
      var that = this;
      var areaList = this.data.areaList;
      var areaName = this.data.areaName;
      //找到id;
      var id = 0;
      for(var i=0;i<areaList.length;i++){
        for (var j = 0; j < areaList[i].areaVoList.length; j++) {
          if (areaName == areaList[i].areaVoList[j].areaName){
            id = areaList[i].areaVoList[j].areaId;
            break;
          }
        }
      }
      //根据id 回显数据
      this.getPriceList(id);
    },
    //根据citysIndex显示省市区;
    showAreaData(){
      //hgz/address/queryCityAndAreaList
      var that = this;
      app.request.postHeadForm('hgz/address/queryCityAndAreaList')
      .then(res =>{
        var citysIndex = that.data.citysIndex;
        var area = res.data;
        var areaList = [
          [],
          []
        ];
        for (var i = 0; i < area.length; i++) {
          areaList[0].push(area[i].cityName);
          if (i == citysIndex[0]) {
            for (var j = 0; j < area[i].areaVoList.length; j++) {
              areaList[1].push(area[i].areaVoList[j].areaName);
            }
          }
        }
        that.setData({
          cityAreaArray: areaList,
          areaList: res.data
        })
      })
    },
    bindRegionColumnChange(e){
      //滑动事件触发
      var that = this;
      var citysIndex = this.data.citysIndex;
      citysIndex[e.detail.column] = e.detail.value;
      that.setData({
        citysIndex: citysIndex
      })
      if (e.detail.column==0){
        that.showAreaData();
      }
    },
    bindRegionChange(e) {
      //确认按钮触发
      var areabyte = e.detail.value;
      var areaList = this.data.cityAreaArray[1];
      var areaName = areaList[areabyte[1]];
      // var i = areabyte[0];
      // var j = areabyte[1];
      // var areaId = this.data.areaList[i].areaVoList[j].areaId;
      this.setData({
        areaName: areaName
      })
      this.getAddressPriceList();
    },
    bindBannerchange(e){
      this.setData({ current: e.detail.current })
    },
    bindDateChange(e){
      this.setData({
        date:e.detail.value
      })
    }
  }
})
